package com.ly.airline.controller;

import com.ly.airline.pojo.Admin;
import com.ly.airline.service.AdminService;
import com.ly.airline.utils.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ：xxl
 * @ClassName AdminController
 * @date ：Created in 2022/4/28 14:11
 * @description： 管理员接口
 * @modified By：
 * @version: 1.0
 */
@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     *@param model
	* @param request
     * @return java.lang.String
     * @author xxl
     * @description: 获得管理员信息并跳转页面
     * @date 2022/4/28 15:24
     */
    @GetMapping("getUserInfo")
    public String getUserInfo(Model model, HttpServletRequest request){
        Admin admin = (Admin) request.getSession().getAttribute("admin");
//        System.out.println(admin);
        return "admin/userinfo/userPw";
    }

    /**
     *@param password
	* @param newPwd
	* @param request
     * @return com.ly.airline.utils.HttpResult
     * @author xxl
     * @description: 更新管理员密码
     * @date 2022/4/28 15:23
     */
    @PostMapping("updatePassword")
    @ResponseBody
    public HttpResult updatePassword(String password,String newPwd,HttpServletRequest request){
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (!admin.getUserpw().equals(password)){
            return HttpResult.success(false,"原密码错误");
        }
        admin.setUserpw(newPwd);
        adminService.updateById(admin);
        return HttpResult.success(true,"修改成功");
    }
}
